package com.streetlight.lampcursor.ui.control

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.streetlight.lampcursor.model.LightMode
import com.streetlight.lampcursor.model.LightStatus
import com.streetlight.lampcursor.model.OperationResult
import com.streetlight.lampcursor.repository.StreetLightRepository
import kotlinx.coroutines.launch

class ControlViewModel : ViewModel() {

    private val repository = StreetLightRepository()

    private val _lightStatus = MutableLiveData<LightStatus>(LightStatus.OFF)
    val lightStatus: LiveData<LightStatus> = _lightStatus

    private val _lightMode = MutableLiveData<LightMode>(LightMode.MANUAL)
    val lightMode: LiveData<LightMode> = _lightMode

    private val _brightness = MutableLiveData<Int>(50)
    val brightness: LiveData<Int> = _brightness

    private val _operationResult = MutableLiveData<OperationResult>()
    val operationResult: LiveData<OperationResult> = _operationResult

    fun turnOnLight() {
        viewModelScope.launch {
            try {
                val result = repository.turnOnLight()
                if (result) {
                    _lightStatus.value = LightStatus.ON
                    _operationResult.value = OperationResult.Success("路灯已开启")
                } else {
                    _operationResult.value = OperationResult.Error("开启路灯失败")
                }
            } catch (e: Exception) {
                _operationResult.value = OperationResult.Error("网络错误: ${e.message}")
            }
        }
    }

    fun turnOffLight() {
        viewModelScope.launch {
            try {
                val result = repository.turnOffLight()
                if (result) {
                    _lightStatus.value = LightStatus.OFF
                    _operationResult.value = OperationResult.Success("路灯已关闭")
                } else {
                    _operationResult.value = OperationResult.Error("关闭路灯失败")
                }
            } catch (e: Exception) {
                _operationResult.value = OperationResult.Error("网络错误: ${e.message}")
            }
        }
    }

    fun setAutoMode() {
        viewModelScope.launch {
            try {
                val result = repository.setAutoMode()
                if (result) {
                    _lightMode.value = LightMode.AUTO
                    _lightStatus.value = LightStatus.AUTO
                    _operationResult.value = OperationResult.Success("已切换到自动模式")
                } else {
                    _operationResult.value = OperationResult.Error("切换自动模式失败")
                }
            } catch (e: Exception) {
                _operationResult.value = OperationResult.Error("网络错误: ${e.message}")
            }
        }
    }

    fun setManualMode() {
        viewModelScope.launch {
            try {
                val result = repository.setManualMode()
                if (result) {
                    _lightMode.value = LightMode.MANUAL
                    _operationResult.value = OperationResult.Success("已切换到手动模式")
                } else {
                    _operationResult.value = OperationResult.Error("切换手动模式失败")
                }
            } catch (e: Exception) {
                _operationResult.value = OperationResult.Error("网络错误: ${e.message}")
            }
        }
    }

    fun setBrightness(brightness: Int) {
        viewModelScope.launch {
            try {
                val result = repository.setBrightness(brightness)
                if (result) {
                    _brightness.value = brightness
                    _operationResult.value = OperationResult.Success("亮度已调节到 $brightness%")
                } else {
                    _operationResult.value = OperationResult.Error("调节亮度失败")
                }
            } catch (e: Exception) {
                _operationResult.value = OperationResult.Error("网络错误: ${e.message}")
            }
        }
    }

    fun setAutoBrightness(enabled: Boolean) {
        viewModelScope.launch {
            try {
                val result = repository.setAutoBrightness(enabled)
                if (result) {
                    _operationResult.value = OperationResult.Success(
                        if (enabled) "自动亮度已开启" else "自动亮度已关闭"
                    )
                } else {
                    _operationResult.value = OperationResult.Error("设置自动亮度失败")
                }
            } catch (e: Exception) {
                _operationResult.value = OperationResult.Error("网络错误: ${e.message}")
            }
        }
    }

    fun setMotionSensor(enabled: Boolean) {
        viewModelScope.launch {
            try {
                val result = repository.setMotionSensor(enabled)
                if (result) {
                    _operationResult.value = OperationResult.Success(
                        if (enabled) "人体感应已开启" else "人体感应已关闭"
                    )
                } else {
                    _operationResult.value = OperationResult.Error("设置人体感应失败")
                }
            } catch (e: Exception) {
                _operationResult.value = OperationResult.Error("网络错误: ${e.message}")
            }
        }
    }

    fun setWeatherAdaptation(enabled: Boolean) {
        viewModelScope.launch {
            try {
                val result = repository.setWeatherAdaptation(enabled)
                if (result) {
                    _operationResult.value = OperationResult.Success(
                        if (enabled) "天气适应已开启" else "天气适应已关闭"
                    )
                } else {
                    _operationResult.value = OperationResult.Error("设置天气适应失败")
                }
            } catch (e: Exception) {
                _operationResult.value = OperationResult.Error("网络错误: ${e.message}")
            }
        }
    }

    fun refreshStatus() {
        viewModelScope.launch {
            try {
                val status = repository.getLightStatus()
                _lightStatus.value = status
                
                val mode = repository.getLightMode()
                _lightMode.value = mode
                
                val brightness = repository.getBrightness()
                _brightness.value = brightness
                
            } catch (e: Exception) {
                _operationResult.value = OperationResult.Error("获取状态失败: ${e.message}")
            }
        }
    }
} 